@REM Copyright (C) Microsoft Corporation. All rights reserved.

setlocal enabledelayedexpansion

  
@REM The path to the WiX directory
set WiXPath=%~1

@REM The Product_Guid is stored in the TFSAdmin.wxs file and the TFSAdmin Installer Design Document
set PRODUCT_GUID={FEF82D2E-206A-4938-8C01-BA3C9FCE2F2C}

@REM Setup MSI File
set SETUP_MSI="%~dp0TFSAdminToolSetup.msi"

@REM WixObj File
set WIX_OBJ="%~dp0TFSAdminToolSetup.wixobj"


@REM Delete the previous MSI, if present.
if exist %SETUP_MSI% (
del /F %SETUP_MSI% )

@REM Attempt to uninstall TFSAdmin - will fail if TFSAdmin isn't already installed.
msiexec /quiet /x %PRODUCT_GUID%
if "!ERRORLEVEL!"=="0" (
  echo "TFSAdmin uninstalled successfully using the Product GUID"
) else (
  echo "TFSAdmin uninstall failed using the Product GUID" 
)

@REM Compile the wxs file
"!WixPath!\candle.exe" -out %WIX_OBJ% "%~dp0TFSAdmin.wxs"
if "!ERRORLEVEL!"=="0" (
  echo "Candle generated %WIX_OBJ%"
) else (
  echo "Candle returned !ERRORLEVEL!"
  exit /b !ERRORLEVEL!
)

@REM Build the installer
"!WixPath!\light.exe" -out %SETUP_MSI% %WIX_OBJ% "!WixPath!\lib\wixui_FeatureTree.wixlib" -loc "!WixPath!\lib\WixUI_en-us.wxl"
if "!ERRORLEVEL!"=="0" (
  echo "Light generated %SETUP_MSI%"
) else (
  echo "Light returned !ERRORLEVEL!"
  exit /b !ERRORLEVEL!
)

@REM Install TFSAdmin
msiexec /quiet /i %SETUP_MSI%
if "!ERRORLEVEL!"=="0" (
  echo "TFSAdmin installed successfully"
) else (
  echo "TFSAdmin failed to install" 
  exit /b !ERRORLEVEL!
)

@REM Check if TFSAdminTool.exe is actually installed.
if not exist "%ProgramFiles%\Power Toys for Visual Studio\TFS Administration Tool\TFSAdminTool.exe" (
  echo "TFSAdmin installer claimed success but the TFSAdmin.exe is not present where expected."
  exit /b 1
)

exit /b !ERRORLEVEL!
